<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends NavController {
    public function index(){
         $goodsModel = D('Admin/Goods');
         $goods1 = $goodsModel->getPromoteGoods();
         $goods2  = $goodsModel->getRecGoods('is_new');
         $goods3  = $goodsModel->getRecGoods('is_hot');
         $goods4  = $goodsModel->getRecGoods('is_best');
         //取出首页楼层的数据
        $catModel = D('Admin/Category');
        $floorData= $catModel->floorData();
        //var_dump($floorData[0]['recSubCat']);exit;
         $this->assign(array(
             'floorData'=>$floorData,
             'goods1' =>$goods1,
             'goods2' =>$goods2,
             'goods3' =>$goods3,
             'goods4' =>$goods4,
             )
         );
            $this->assign(
                array(
                    '_show_nav'=>1,
                    '_page_title'=>'首页',
                    '_page_keywords'=>'首页',
                    '_page_description'=>'首页'
            ));
        $this->display();
    }
    public function goods(){
        //接收商品Id
        $id = I('get.id');
        $gModel = D('Admin/Goods');
        $info = $gModel->find($id);
        $catModel = D('Admin/Category');
        $catPath = $catModel->parentPath($info['cat_id']);
        //相册
        $gpModel =  D('goods_pic');
        $gpData = $gpModel->where('goods_id ='.$id )->select();
        //商品属性
        $gaModel = D('goods_attr');
        $gaData = $gaModel->alias('a')->field('a.*,b.attr_name,b.attr_type')
            ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id = b.id')
            ->where('a.goods_id='.$id)->select();
        $uniArr = array();
        $muiArr = array();
        foreach($gaData as $k => $v){
            if($v['attr_type']=='only')
               $uniArr[] = $v;
            else
                $muiArr[$v['attr_name']][]=$v;
        }
        //商品会员价格
        $mpModel = D('member_price');
        $mpData = $mpModel->alias('a')->field('a.price,b.level_name')
            ->join('JOIN __MEMBER_LEVEL__ b ON a.level_id=b.id')
            ->where('a.goods_id ='.$id)->select();
        $this->assign(
            array(
                'info'=>$info,
                'catPath'=>$catPath
            )
        );
        $viewPath = C('IMAGE_CONFIG');
        $this->assign(
            array(
                'info'=>$info,
                'catPath'=>$catPath,
                'gpData' =>$gpData,
                'uniArr'=>$uniArr,
                'mulArr'=>$muiArr,
                'mpData'=>$mpData,
                'viewPath'=>$viewPath['viewPath'],
                '_goods_nav'=>1,
                '_page_title'=>'商品详情页',
                '_page_keywords'=>'商品详情页',
                '_page_description'=>'商品详情页'
            )
        );
        $this->display();
    }
    public function displayHistory(){
        $data = isset($_COOKIE['display_history']) ? unserialize($_COOKIE['display_history']) : array();
        $id= I('get.id');
        if(!empty($id)){
            array_unshift($data,$id);
            $data = array_unique($data);
            if(count($data)>6){
                $data = array_slice($data,0,4);
            }
            setcookie('display_history',serialize($data),time()+30 * 86400,'/');
        }
        $gModel = D('Admin/Goods');
        $data = implode(',',$data);
        $gData = $gModel->field('id,mid_logo,goods_name')->where(
            array(
                'id' =>array('in',$data),
                'is_on_sale'=>array('eq','Y')
            )
        )->order("FIELD('id',$data)")->select();
        echo json_encode($gData);
    }
    public function ajaxGetMemberPrice(){
        $goodsId=I('get.goods_id');
        $gpModel = D('Admin/Goods');
        echo $gpModel->getMemberPrice($goodsId);
    }
}